//baseline simulations


import delimited "$dir/Model/simulated_data/simulated_data_base_2.csv", clear
gen race = 1
tempfile whites
save `whites'

import delimited "$dir/Model/simulated_data/simulated_data_base_3.csv", clear
gen race = 2
append using `whites'
export delimited "$dir/Model/simulated_data/simulated_data_base_combined.csv", replace novarn




//sample
import delimited "$model/utilities/estimation_sample.csv", clear
//import delimited "$model/utilities/simulation_sample.csv", clear
ren v1 uniqid
ren v2 year
ren v3 mu
ren v4 e
ren v5 m
ren v6 p
ren v7 l
ren v8 age
ren v9 x
ren v10 ac
ren v11 f
ren v12 lp_type
ren v13 h
ren v14 wage
ren v15 l_prime
drop if e==2 & age<4

//likelihood of moving to/from home, broken down by fertilty type
gen mig = (l_prime<11)
replace mig = . if l_prime == 99
gen home = (l == 1)
gen home_mig = (l_prime == 1)

tab home_mig if mig == 1

//fertility types
gen famtype = .
replace famtype = 1 if f == 1 & ac == 1
replace famtype = 2 if f == 2 & ac == 1
replace famtype = 3 if ac!=1

cap log close
log using "$output/Table_11a", replace

noi di "Migration from home, data"
*****migration from home
noi tab mig if home
noi tab mig if home & famtype == 1
noi tab mig if home & famtype == 2
noi tab mig if home & famtype == 3

noi di ""
noi di "Migration to home, data"
***migration to home
noi tab home_mig if !home
noi tab home_mig if !home & famtype == 1
noi tab home_mig if !home & famtype == 2
noi tab home_mig if !home & famtype == 3
log close

di "Rate of repeated migration in estimation sample"
collapse (sum) mig, by(uniqid)
gen multimig = (mig>1)
tab multimig






//OOS
use "$temp/acs_base", clear

local amens `"college unemp violent property pop govt_exp s_t_ratio cooling"'
local amens_prev `"college_prev unemp_prev violent_prev property_prev pop_prev govt_exp_prev s_t_ratio_prev cooling_prev"'
local amens_bpl `"college_bpl unemp_bpl violent_bpl property_bpl pop_bpl govt_exp_bpl s_t_ratio_bpl cooling_bpl"'


local controls `"i.year black hispan age* hs some_coll coll grad `amens_prev' married i.bpl"'

*****************Begin Running Linear Probability Models*****************
************effects of first child on home migration************
replace home_mig = home_mig*100 //conversion to 100 -- eases interpretation
//su home_mig if !same_state_prev [aweight = perwt]
//su home_mig if !same_state_prev & married [aweight = perwt]
//su home_mig if !same_state_prev & !married [aweight = perwt]


cap log close
log using "$output/Table_11b", replace
noi tab mig if same_state_prev [aweight = perwt]
noi tab mig if same_state_prev & !baby & !preg_first
noi tab mig if same_state_prev & preg_first
noi tab mig if same_state_prev & baby & !preg_first



noi tab home_mig if !same_state_prev [aweight = perwt]
noi tab home_mig if !same_state_prev & !baby & !preg_first
noi tab home_mig if !same_state_prev & preg_first
noi tab home_mig if !same_state_prev & baby & !preg_first
log close



//simulated data
import delimited "$model/simulated_data/simulated_data_base_combined.csv", clear
//import delimited "$model/simulated_data/simulated_data_base.csv", clear
ren v1 uniqid
ren v2 year
ren v3 mu
ren v4 e
ren v5 m
ren v6 p
ren v7 l
ren v8 age
ren v9 x
ren v10 ac
ren v11 f
ren v12 lp_type
ren v13 h
ren v14 wage
ren v15 l_prime
ren v16 sim
//drop if e==2 & age<4

//likelihood of moving to/from home, broken down by fertilty type
gen mig = (l_prime<11)
replace mig = . if l_prime == 99
gen home = (l == 1)
gen home_mig = (l_prime == 1)


preserve
import delimited "$dir/Model/utilities/div_chars.csv", clear
keep v1 v3
ren v1 l_prime_div
ren v3 lp_pop 
tempfile pops
save `pops'
restore

//mover statistics: education, amrital status, kid, age, destination state population
gen married = (m>1)
replace e = e-1
gen kid = (ac>1)
gen l_prime_div = .
replace l_prime_div = lp if mig & l_prime == 1 //parent destination
replace l_prime_div = l_prime - 1 if l_prime > 1 & l_prime< 11
merge m:1 l_prime_div using `pops', keep(1 3) nogen

//
su e if mig
su married if mig
su kid if mig
su age if mig
su lp_pop if mig






tab home_mig if mig == 1

//fertility types
gen famtype = .
replace famtype = 1 if f == 1 & ac == 1
replace famtype = 2 if f == 2 & ac == 1
replace famtype = 3 if ac!=1


cap log close
log using "$output/Table_11c", replace
noi di ""
noi di "Migration from home, model"
*****migration from home
noi tab mig if home
noi tab mig if home & famtype == 1
noi tab mig if home & famtype == 2
noi tab mig if home & famtype == 3

noi di ""
noi di "Migration to home, model"
***migration to home
noi tab home_mig if !home
noi tab home_mig if !home & famtype == 1
noi tab home_mig if !home & famtype == 2
noi tab home_mig if !home & famtype == 3
log close


di "Rate of multiple migration in model"
collapse (sum) mig, by(uniqid sim)
gen multimig = (mig>1)
tab multimig





//exit